from quant_researcher.quant.datasource_fetch.crypto_api.aave import get_aave_borrow_fee_rate
from quant_researcher.quant.project_tool.time_tool import get_yesterday
from quant_researcher.quant.datasource_fetch.crypto_api.glassnode import get_prices
import numpy as np
import os
import time
from utils import df_into_db


end_date = get_yesterday(marker='with_n_dash')  # 计算截止昨日收盘
price_log_price = get_prices(ohlc=False, asset='BTC', start_date='2018-01-01', end_date=end_date)
price_log_price['log_prices'] = np.log10(price_log_price['close'])
# 分析去中心化借贷平台aave
for asset in ['USDC', 'DAI', 'ETH', 'WBTC']:
    df = get_aave_borrow_fee_rate(asset=asset)
    df = df.groupby(['date'])[df.columns[:4]].mean()
    all_frame = df.merge(price_log_price, left_index=True, right_index=True)
    all_frame['asset'] = asset
    all_frame = all_frame.reset_index(drop=False)
    all_frame.rename(columns={'index': 'date'}, inplace=True)
    df_into_db(all_frame, db_name="margin_interest_rate", table_name="aave_margin_interest_rate")
    time.sleep(5)